package com.qall.monitorserver.service.node;


import com.alibaba.dubbo.config.annotation.Service;
import com.qall.monitorability.entity.NodeEntity;
import com.qall.monitorability.service.NodeService;

import com.qall.monitorserver.service.node.mapper.entity.NodeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Component
public class NodeServiceImpl implements NodeService {


    @Resource
    private NodeMapper nodeMapper;

    @Override
    public List<NodeEntity> getAllNode() {
        return new ArrayList<>();
    }

    @Override
    public List<NodeEntity> getNodeByNameSpace(String nameSpace) {
        return new ArrayList<>();
    }

    @Override
    public List<NodeEntity> getNodeByAppName(String appName) {
        return new ArrayList<>();
    }

    @Override
    public void notifyNodeChange(List<NodeEntity> nodeEntityList) {

    }

    @Override
    public void reportNodeInfo(NodeEntity nodeEntity) {
        com.qall.monitorserver.service.node.mapper.entity.NodeEntity entity = new com.qall.monitorserver.service.node.mapper.entity.NodeEntity();

        BeanUtils.copyProperties(nodeEntity, entity);
        com.qall.monitorserver.service.node.mapper.entity.NodeEntity nodeEntityTmp = nodeMapper.selectByNameAndIdIp(nodeEntity.getAppName(), nodeEntity.getNodeIp());
        if (nodeEntityTmp==null) {
            nodeMapper.insert(entity);
        } else {
         com.qall.monitorserver.service.node.mapper.entity.NodeEntity nodeEntity1=new com.qall.monitorserver.service.node.mapper.entity.NodeEntity();
            nodeEntity1
                    .setHealthInfo(nodeEntity.getHealthInfo())
                    .setProperties(nodeEntity.getProperties())
                    .setNodeIp(nodeEntity.getNodeIp())
                    .setAppName(nodeEntity.getAppName())
                    .setUpdateTime(nodeEntity.getUpdateTime());

            nodeMapper.updateByNode(nodeEntity1);
        }

    }
}
